package com.solution.pc.shoppingcart.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.solution.common.dao.BaseDao;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;
import com.solution.common.service.impl.BaseServiceImpl;
import com.solution.pc.shoppingcart.dao.ShoppingCartDao;
import com.solution.pc.shoppingcart.entity.ShoppingCartEntity;
import com.solution.pc.shoppingcart.service.ShoppingCartService;

/**
 * 购物车service实现层
 * @author lilp
 * @date 2017-10-13 20:05:27
 */
@Service
public class ShoppingCartServiceImpl extends BaseServiceImpl<ShoppingCartEntity> implements ShoppingCartService {

	/**
	 * 购物车接口
	 */
	@Autowired
	private ShoppingCartDao shoppingCartDao;
	
	protected BaseDao<ShoppingCartEntity> getDao() {
		return this.shoppingCartDao;
	}

	/**
	 * 保存购物车
	 * @param shoppingCartEntity 购物车实体
	 * @return Long 添加成功购物车的ID
	 * @author lilp
	 * @date 2017-10-13 20:05:27
	 */
	@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
	public long saveShoppingCart(ShoppingCartEntity shoppingCartEntity) {
		ShoppingCartEntity sce = null;
		// 如果购物车不存在该商品，则添加，如果都存在则修改数量
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("productId", shoppingCartEntity.getProductId());
		param.put("userCode", shoppingCartEntity.getUserCode());
		param.put("productSpec", shoppingCartEntity.getProductSpec());
		List<ShoppingCartEntity> cartList = shoppingCartDao.listBy(param);
		if(cartList != null && cartList.size() == 1){
			sce = cartList.get(0);
			// addFlag =1 ，代表从详情页面添加到购物车，数量=原数量+页面传入的数量
			if("1".equals(shoppingCartEntity.getAddFlag())){
				sce.setBuyNum(sce.getBuyNum()+shoppingCartEntity.getBuyNum());
			}else{
				// 修改购物车数量(购物车页面的+-号操作)
				sce.setBuyNum(shoppingCartEntity.getBuyNum());
			}
			
			if("1".equals(shoppingCartEntity.getDirectBuy())){
				// 立即购买，购买数量为客户选择的数量
				sce.setBuyNum(shoppingCartEntity.getBuyNum());
			}
			
			// 修改商品数量
			shoppingCartDao.update(sce);
			return sce.getId();
		}else if(cartList.size() > 1){
			// 合并记录(以防万一)
			sce = cartList.get(0);
			int buyNum = 0;
			// 删除原有记录，插入一条新的记录
			for (ShoppingCartEntity product : cartList) {
				buyNum += product.getBuyNum();
				shoppingCartDao.deleteById(product.getId());
			}
			sce.setBuyNum(buyNum);
			long id = shoppingCartDao.insert(sce);
			return id;
		}else{
			long id = shoppingCartDao.insert(shoppingCartEntity);
			return id;
		}
	}

	/**
	 * 分页查询购物车
	 * @param pageParam 分页参数
	 * @param paramMap 查询参数
	 * @return PageBean 分页参数和分页数据
	 * @author lilp
	 * @date 2017-10-13 20:05:27
	 */
	@Override
	public PageBean listShoppingCartPage(PageParam pageParam, Map<String, Object> paramMap) {
		return shoppingCartDao.listShoppingCartPage(pageParam, paramMap);
	}
	
	/**
	 * 根据ID查询用户信息
	 * @param id 用户id
	 * @return shoppingCartEntity 用户对象
	 * @author lilp
	 * @date 2017-10-13 20:05:27
	 */
	public ShoppingCartEntity getShoppingCartById(Long id) {
		return shoppingCartDao.getById(id.longValue());
	}

	/**
	 * 更新用户
	 * @param shoppingCartEntity 用户实体
	 * @return Long 更新是否成功，>1代表更新成功
	 * @author lilp
	 * @date 2017-10-13 20:05:27
	 */
	@Override
	public int updateShoppingCart(ShoppingCartEntity shoppingCartEntity) {
		return shoppingCartDao.updateShoppingCart(shoppingCartEntity);
	}
	
	
	/**
	 * 根据条件查询购物车
	 * @param param
	 * @return 数据列表
	 * @author lilp
	 * @date 2017-10-13 20:05:27
	 */
	@Override
	public List<ShoppingCartEntity> listShoppingCart( Map<String, Object> param){
		return  shoppingCartDao.listShoppingCart(param);
	}
	
	/**
	 * 删除购物车
	 * @param 
	 * @return int 返回删除记录条数
	 * @author lilp
	 * @date 2017-10-13 20:05:27
	 */
	public int deleteShoppingCartById(long id){
		return  shoppingCartDao.deleteById(id);
	}
	
	/**
	 * 批量删除购物车商品
	 * @param param
	 * @return                                                                                                                 
	 * @author llp
	 */
	public int deleteShoppingCartProduct(Map<String, Object> param){
		return shoppingCartDao.deleteShoppingCartProduct(param);
	}
	
	
	/**
	 * 将匿名登录的购物车商品添加到已登录的购物车中
	 * @param param
	 */
	public void transferShoopingcartProduct(Map<String, Object> param){
		List<ShoppingCartEntity>  itemList = shoppingCartDao.listBy(param);
		if(itemList != null && itemList.size() > 0){
			String userCode = (String)param.get("loginUserCode");
			param.clear();
			param.put("userCode", userCode);
			param.put("itemIds", itemList);
			shoppingCartDao.transferShoopingcartProduct(param);
		}
	}
}
